package leetcode.leetcode_3147;
//  dp[i] = max(dp[i-1],dp[i-k]+energy[i])
class Solution {
    public int maximumEnergy(int[] energy, int k) {
        int n = energy.length;
        int[] sums = new int[n];
        for (int i =0 ;i<n;i++){
            if (i>=k){
                sums[i] = Math.max(sums[i-k]+energy[i],energy[i]);
            }
            else {
                sums[i] = energy[i];
            }
        }
        int ans = Integer.MIN_VALUE;
        for (int i = n-k;i<n;i++){
            ans = Math.max(ans,sums[i]);
        }
        return ans;

    }

    public static void main(String[] args) {
//        [5,2,-10,-5,1] k=3
        int[] energy = {5,2,-10,-5,1};
        System.out.println(new Solution().maximumEnergy(energy,3));
    }
}